<script setup lang='ts'>
  import { onMounted, reactive } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { Toastloading } from '@/utils/public';
  import { getDateArray, getPlayEndTime } from '@/utils'
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import table2excel from 'js-table2excel';
  const action = reactive({
    total:0,
    list:[] as any[],
    statistics:[] as any[],
    drawType:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymdhis'))
  })
  const initialQuery = {
    page:1,
    limit:20,
    uuid:'',
    gender:'',
    luckId:'',
  }
  const listQuery = reactive({
    ...initialQuery
  })
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const resetQuery = ()=>{
    action.timeObj = getPlayEndTime(getDateArray('ymdhis'))
    Object.assign(listQuery,{
      ...initialQuery
    })
    getList()
  }
  const getDrawType = async()=>{
    const res = await luckDrawApi.getDrawTypeList()
    const { data=[] } = res
    data.unshift({ id: "", name: "全部" });
    action.drawType = data
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime, endTime } = action.timeObj
    const res = await luckDrawApi.getUserStatistic({
      startDate:playTime,
      endDate:endTime,
      ...listQuery
    })
    loading.close()
    const { data=[], total=0, all=[] } = res.data
    action.statistics = all
    action.total = total
    action.list = data
  }
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中')
    const { playTime, endTime } = action.timeObj
    const res = await luckDrawApi.getUserStatistic({
      page:1,
      startDate:playTime,
      endDate:endTime,
      limit:3000
    })
    loading.close()
    const { data=[] } = res.data
    const list = data.map((res:any)=>{
      if(res['output_ratio'] = `${res.output_ratio}%`)
      return res
    })
    const tHeader = [
      { title: '用户UUID', key: "uuid"},
      { title: '昵称', key: "nickname"},
      { title: '总投入（钻石）', key: "luck_price"},
      { title: '总产出（钻石）', key: "award_price"},
      { title: '投入产出比', key: "output_ratio"},
      { title: '损益', key: "profit"},
    ]
    table2excel(
      tHeader,
      list,
      `许愿池报表`)
  }
  onMounted(()=>{
    getDrawType()
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <el-input v-model="listQuery.uuid" placeholder="请输入用户UUID" />
        </div>
        <div class="labelItem">
          <div class="label">性别：</div>
          <el-select
            v-model="listQuery.gender"
            placeholder="请选择性别"
          >
            <el-option label="全部" value="" />
            <el-option label="男" value="1" />
            <el-option label="女" value="2" />
            <el-option label="未知" value="3" />
          </el-select>
        </div>
        <div class="labelItem">
          <div class="label">许愿池类型：</div>
          <el-select
            v-model="listQuery.luckId"
            placeholder="请选择许愿池类型"
          >
            <el-option
              v-for="(item, index) in action.drawType"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </div>
        <xmDatePicker v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">查询</el-button>
          <el-button type="warning" @click="handleDownload">导出列表</el-button>
        </div>
      </div>
    </div>
    <el-table
      style="min-height: 120px"
      class="xm-TableCenter"
      :data="action.statistics"
      border
    >
      <el-table-column width="120" prop="luck_name" label="许愿池名称" />
      <el-table-column min-width="150" prop="all_luck_price" label="总投入（钻石）" />
      <el-table-column min-width="120" prop="all_award_price" label="总产出（钻石）" />
      <el-table-column min-width="120" prop="output_ratio" label="产出比例">
        <template #default="scoped">
          {{ scoped.row.output_ratio }}%
        </template>
      </el-table-column>
      <el-table-column min-width="120" prop="all_user_num" label="参与人数" />
    </el-table>
    <el-table
      style="margin-top:40px;"
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
      show-summary
    >
      <el-table-column width="120" prop="uuid" label="用户UUID" />
      <el-table-column min-width="150" prop="nickname" label="昵称" />
      <el-table-column min-width="120" prop="luck_name" label="许愿池名称" />
      <el-table-column min-width="120" sortable prop="luck_price" label="投入（钻石）" />
      <el-table-column min-width="120" sortable prop="award_price" label="产出（钻石）" />
      <el-table-column min-width="100" sortable prop="output_ratio" label="投入产出比">
        <template #default="scoped">
          {{ scoped.row.output_ratio }}%
        </template>
      </el-table-column>
      <el-table-column min-width="120" sortable prop="profit" label="损益" />
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
  </div>
</template>
